//
//  TopMenusView.m
//  SUZHOUBANK
//
//  Created by apple on 16/4/12.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "TopMenusView.h"
#import "ScreenAdaptation.h"
#import "TopMenusCollectionViewCell.h"
static NSString *kCollectionViewCellIdentifier = @"kCollectionViewCellIdentifier";
@interface TopMenusView() <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic,strong)UIImageView *leftHide;
@property (nonatomic,strong)UIImageView *rightHide;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@end

@implementation TopMenusView
- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource {
    
    if (self = [super initWithFrame:frame]) {
        self.dataSource = dataSource;
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.collectionView];
        [self addSubview:self.leftHide];
        self.leftHide.hidden = YES;
        [self addSubview:self.rightHide];
        if (self.collectionView.indexPathsForVisibleItems.firstObject.item <= (self.dataSource.count - 1)) {
            self.rightHide.hidden = NO;

        } else {
            
            self.rightHide.hidden = YES;
        }
    }
    return self;
}

#pragma mark *** UICollectionViewDataSource ***

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopMenusCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];
    [cell.itemButton setTitle:self.dataSource[indexPath.row] forState:UIControlStateNormal];
    [cell.itemButton addTarget:self action:@selector(respondsToItemBtn:event:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}

- (void)respondsToItemBtn:(UIButton *)sender event:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    if (_delegate && [_delegate respondsToSelector:@selector(topMenusView:didSelectedItemIndexPathRow:)]) {
        [_delegate topMenusView:self didSelectedItemIndexPathRow:indexPath.row];
    }
}
#pragma mark *** UICollectionViewDelegate ***

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(5, 0, 5, 0);
}

#pragma mark *** ScrollViewDelegate ***

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@" x :%lf",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x >= 140) {
        self.leftHide.hidden = NO;
    } else if (scrollView.contentOffset.x < 140) {
        self.leftHide.hidden = YES;
    }
    CGPoint offset = scrollView.contentOffset;

    CGRect bounds = scrollView.bounds;
    
    CGSize size = scrollView.contentSize;
    
    CGFloat currentOffset = offset.x + bounds.size.width;
    
    CGFloat maximumOffset = size.width;

    if (currentOffset >= (maximumOffset - 50)) {
        self.rightHide.hidden = YES;
    } else self.rightHide.hidden = NO;
//    NSLog(@"item.Num : %ld",self.collectionView.indexPathsForVisibleItems.firstObject.row);

}
#pragma mark *** Getter ***
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        [_collectionView registerClass:[TopMenusCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellIdentifier];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)flowLayout {
    
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = AdaptationSize(100, 30);//设置 item 大小
        _flowLayout.minimumInteritemSpacing = 20.0f;//设置 item 之间的间距
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//设置横向滚动
    }
    return _flowLayout;
}

- (UIView *)leftHide {
    
    if (!_leftHide) {
        _leftHide = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, self.frame.size.height)];
        [_leftHide setImage:[UIImage imageNamed:@"菜单左拉"]];
    }
    return _leftHide;
}
- (UIImageView *)rightHide {
    
    if (!_rightHide) {
        _rightHide = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.frame) - 20, 0, 20, self.frame.size.height)];
        [_rightHide setImage:[UIImage imageNamed:@"菜单右拉"]];
    }
    return _rightHide;
}
@end
